package com.zhao.controller.show;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhao.Util.HttpContextUtil;
import com.zhao.Util.IPUtil;
import com.zhao.comm.log.MyLog;
import com.zhao.pojo.Datafile;
import com.zhao.pojo.FileComment;
import com.zhao.pojo.Filestatistic;
import com.zhao.pojo.User;
import com.zhao.service.DatafileService;
import com.zhao.service.FileCommentService;
import com.zhao.service.FileStatisticService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;

@Controller
@RequestMapping("/fileComment")
public class FileCommentController {

    @Autowired
    FileCommentService fileCommentService;
    @Autowired
    FileStatisticService fileStatisticService;
    @Autowired
    DatafileService datafileService;

    @ResponseBody
    @RequestMapping("/comment/{id}/{xing}/{bauthor}")
    public String comment(@PathVariable Integer id,
                          @PathVariable String xing,
                          String content,
                          @PathVariable String bauthor,
                          String ip,
                          String ipHome
    ){
        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        FileComment fileComment = new FileComment();
        //获取当前用户
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        if ("".equals(content)||content==null){
            content="他只是冒个泡，并没有留言";
        }
        FileComment one = fileCommentService.getOne(new QueryWrapper<FileComment>().eq("father", 0).eq("file_id", id).eq("author", loginUser.getUserAccount()));
        if (one!=null){
            return "2";//评论过了，不可重复评论
        }
        fileComment.setAuthor(loginUser.getUserAccount());
        fileComment.setFather(0);
        fileComment.setBauthor(bauthor);
        fileComment.setContent(content);
        fileComment.setFileId(id);
        fileComment.setIp(ip);
        fileComment.setIpHome(ipHome);
        if (fileComment.getIp().isEmpty()){
            fileComment.setIp(IPUtil.getIpAddr(request));
        }
        if (fileComment.getIpHome().isEmpty()){
            fileComment.setIpHome("未知");
        }
        fileComment.setScore(xing);
        fileCommentService.save(fileComment);

        Filestatistic file_id = fileStatisticService.getOne(new QueryWrapper<Filestatistic>().eq("file_id", id));
        Datafile byId = datafileService.getById(id);
        if (!loginUser.getUserAccount().equals(byId.getAuthor())){
            file_id.setCommentsNum(file_id.getCommentsNum()+1);
            if (file_id.getAverage() == 0) {
                file_id.setAverage(Integer.parseInt(xing));
            }else {
                file_id.setAverage((Integer.parseInt(xing) + file_id.getAverage()) / 2);
            }
            fileStatisticService.updateById(file_id);
        }
        /*return "redirect:/show/fileContent/"+id;*/
        return "1";
    }
    @ResponseBody
    @RequestMapping("/hui")
    public String hui(FileComment fileComment){

        HttpServletRequest request = HttpContextUtil.getHttpServletRequest();
        User loginUser = (User) SecurityUtils.getSubject().getPrincipal();
        fileComment.setAuthor(loginUser.getUserAccount());
        if (fileComment.getIp().isEmpty()){
            fileComment.setIp(IPUtil.getIpAddr(request));
        }
        if (fileComment.getIpHome().isEmpty()){
            fileComment.setIpHome("未知");
        }
        fileCommentService.save(fileComment);

        Filestatistic file_id = fileStatisticService.getOne(new QueryWrapper<Filestatistic>().eq("file_id", fileComment.getFileId()));
        Datafile byId = datafileService.getById(fileComment.getFileId());
        if (!loginUser.getUserAccount().equals(byId.getAuthor())){
            file_id.setCommentsNum(file_id.getCommentsNum()+1);
            fileStatisticService.updateById(file_id);
        }
        return "1";
    }
}
